// pages/cnt/cnt.js
import http from '../../http'
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		top: '',
		left: '',
		height: '',
		width: '',

		//天气
		weatherUrl: '',
		city: '',
		T: '',
		sky: '',
		//日期时间
		week: '',
		date: '',
		time: '',

		project: '请选择项目',
		projectId: 0,
		projectShow: false,
		projectList: [],

		latitude: '34.84431980960885',
		longitude: '113.21843835278817',
	},
	// 天气
	weather() {
		wx.request({
			url: "https://restapi.amap.com/v3/geocode/regeo?parameters",
			method: "get",
			header: {
				"content-type": "application/json",
			},
			data: {
				key: "77ec77316c52bcaf457cbb232100187f",
				location: this.data.longitude + ',' + this.data.latitude
			},
			success: (res) => {
				console.log('获取本地地址', res.data);
				this.setData({
					adcode: res.data.regeocode.addressComponent.adcode
				}, () => {
					wx.request({
						url: 'https://restapi.amap.com/v3/weather/weatherInfo?parameters',
						method: "get",
						header: {
							"content-type": "application/json",
						},
						data: {
							key: "77ec77316c52bcaf457cbb232100187f",
							city: this.data.adcode,
						},
						success: (res) => {
							console.log('获取本地天气', res.data);
							let data = res.data.lives[0]
							this.setData({
								city: data.city,
								T: data.temperature,
								sky: data.weather
							})
							if (data.weather == '晴') {
								this.setData({
									weatherUrl: '/img/home/clear.png'
								})
							} else if (data.weather == '阴') {
								this.setData({
									weatherUrl: '/img/home/lunar.png'
								})
							} else if (data.weather == '多云') {
								this.setData({
									weatherUrl: '/img/home/cloudy.png'
								})
							} else if (data.weather == "大雨" ||
								data.weather == "中雨" ||
								data.weather == "小雨") {
								this.setData({
									weatherUrl: '/img/home/rain.png'
								})
							} else if (data.weather == "大雪" ||
								data.weather == "中雪" ||
								data.weather == "小雪") {
								this.setData({
									weatherUrl: '/img/home/snow.png'
								})
							} else {
								this.setData({
									weatherUrl: '/img/home/clear.png'
								})
							}
						}
					})
				})

			}
		})
	},
	// 日期时间
	timeGet() {
		var date = new Date(); //时间戳为10位需*1000，时间戳为13位的话不需乘1000
		var M =
			date.getMonth() + 1 + "月";
		var D =
			date.getDate() + "日";
		var h =
			date.getHours() < 10 ?
			"0" + date.getHours() + ":" :
			date.getHours() + ":";
		var min =
			date.getMinutes() < 10 ?
			"0" + date.getMinutes() :
			date.getMinutes();
		var week = new Date().getDay();
		if (week == 1) {
			this.setData({
				week: "周一"
			})
		} else if (week == 2) {
			this.setData({
				week: "周二"
			})
		} else if (week == 3) {
			this.setData({
				week: "周三"
			})
		} else if (week == 4) {
			this.setData({
				week: "周四"
			})
		} else if (week == 5) {
			this.setData({
				week: "周五"
			})
		} else if (week == 6) {
			this.setData({
				week: "周六"
			})
		} else if (week == 7) {
			this.setData({
				week: "周日"
			})
		}
		let data = M + D;
		let strDate = h + min;
		this.setData({
			date: data,
			time: strDate
		})
	},
	projectChange() {
		if (this.data.projectList.length != 0) {
			this.setData({
				projectShow: true,
			});
		} else {
			wx.showToast({
				title: '用户未登录',
				icon: 'error',
			})
		}
	},
	projectClose() {
		this.setData({
			projectShow: false,
		});
	},
	projectSelect(event) {
		console.log(event.detail);
		this.setData({
			project: event.detail.name,
			projectId: event.detail.value
		})
	},
	skip(e) {
		const url = e.currentTarget.dataset.url;
		console.log(url);
		if (this.data.projectId != 0) {
			wx.navigateTo({
				url: `${url}?projectId=${this.data.projectId}`,
			})
		} else {
			if (this.data.projectList.length != 0) {
				wx.showToast({
					title: '请先选择项目',
					icon: 'error',
				})
				this.setData({
					projectShow: true
				})
			} else {
				wx.showToast({
					title: '用户未登录',
					icon: 'error',
				})
			}
		
		}

	},
	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad(options) {

	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow() {
		this.timeGet()
		setInterval(() => {
			this.timeGet()
		}, 10000);
		this.setData({
			top: wx.getStorageSync('customTop'),
			left: wx.getStorageSync('screenWidth') - wx.getStorageSync('customRight'),
			height: wx.getStorageSync('customHeight'),
		})
		http.get({
			url: '/zg/project/list',
			then: (res) => {
				console.log('项目', res);
				if(res.data.code==0){
					let list = res.data.data.filter(item => {
						return item.parentId == 0
					})
					list = list.map(item => {
						return {
							name: item.projectName,
							value: item.id
						}
					})
					this.setData({
						projectList: list
					})
					wx.getLocation({
						type: "gcj02",
						success: (res) => {
							console.log(res);
							this.setData({
								longitude: res.longitude,
								latitude: res.latitude
							}, () => {
								this.weather()
							})
						},
						fail: (error) => {
							this.weather()
							console.log(error);
						}
					})
					
				}else{
					this.setData({
						projectList: []
					})
				}
				
			}
		})
	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage() {

	}
})